﻿#ifndef DBHELPER_H
#define DBHELPER_H

#include <QString>
#include <QSettings>
#include <QSqlDatabase>

#define CFG_MYSQL "../etc/mysql.ini"

static bool createConnection()
{
    QSqlDatabase db;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
        db=QSqlDatabase::database("qt_sql_default_connection");
    else
    {
        QSettings set(CFG_MYSQL,QSettings::IniFormat);
        QString hostName=set.value("mysql/host").toString();
        QString userName=set.value("mysql/user").toString();
        QString passwd=set.value("mysql/pwd").toString();
        QString dbName=set.value("mysql/db").toString();

        db=QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName(hostName);
        db.setDatabaseName(dbName);
        db.setUserName(userName);
        db.setPassword(passwd);
    }
    return db.open();
}
#endif // DBHELPER_H
